<?php
/**
 * /depot/library/models/Htmlhelper/Page.php
 * @author qqinxl
 * 2008/03/24
 * 
 * @category  
 * @package  
 * @copyright  Copyright (c) {}
 */
class Htmlhelper_Page {
	//	<div class="pagination">
	//<p><span><strong>Previous</strong></span> <span>1</span> <a
	//	href="devtodo">2</a> <a href="devtodo">3</a> <a href="devtodo">4</a> <a
	//	href="devtodo">5</a> <a href="devtodo"><strong>Next</strong></a></p>
	//
	//<h4>Page 1 of 5</h4>
	//</div>

	const Link_Start = 'link_start';
	const Link_Back = 'link_back';
	const Link_Next = 'link_next';
	const Link_End = 'link_end';

	const Link_List = 'link_list';
	
	const Total_Page = 'total_page';
	const Now_Page = 'now_page';
	
	const Link = 'link';
	
	const Default_Page_Number = 5; 
	
	protected $_Default_Html_Format = "<span style='text-align:left;float:left;'>Page {now_page} of {total_page}</span><span> {link_start}{link_back}&nbsp;{link_list}&nbsp;{link_next}{link_end}</span>";	
	
	protected $_Default_A_Link_Format = "{link}";
	
	protected $_Default_No_Link_Format = "<span>{link}</span>";
	
	protected $_Default_LinkList_Format = '{link_list}';
	
	protected $_Default_NowLinkList_Format = '<span>{link_list}</span>';
	
	protected $_pageInfoArray = array();
	
	protected $_htmlPageLink = array();
		
	/**
	 * @var Zend_Translate_Adapter
	*/
	protected $_translator;
	
	protected $_startString;
	protected $_endString;
	protected $_backString;
	protected $_nextString;
	
	/**
	 * @var Object_PageInfo
	 * @access protected
	*/
	protected $_pageInfo;
	
	/**
     * Singleton instance
     * @var Htmlhelper_Page
     */
	private static $_instance = null;
/**
   * Constructor
   * @return void
   */
	protected function __construct(){
		if(empty($this->_translator)){
			$this->_translator = Zend_Registry::get("Zend_Translate");
			$this->_startString = $this->_translator->_("page.info.start");
			$this->_endString = $this->_translator->_("page.info.end");
			$this->_backString = $this->_translator->_("page.info.back");
			$this->_nextString = $this->_translator->_("page.info.next");
		}			
	}
	/**
     * Singleton instance 
     * @return Htmlhelper_Page
     */
	public static function getInstance(){
		if (null === self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}	
	/**
	 * 设置页面信息
	 *
	 * @param Object_PageInfo $pageInfo
	 * @return Object_PageInfo
	 */
	public function setPageInfo($pageInfo){
		$this->_pageInfo = $pageInfo;	
		return $this;	
	}	
	/**
	     * 获得分页的html代码	   
	     * @return array 
	 */
	public function getPageHtml(){
		try{
			if(empty($this->_pageInfo)){
				throw new Exception('url info and page info should be set firlst.');
			}
			if($this->_pageInfo->getTotalnumber() <= 1){
				return null;
			}
			$url = $this->_pageInfo->getUrl();
			if(empty($url)){
				throw new Exception('url is null');
			}	
			
			if($this->_isFirstPage()){				
				$pageHtml[self::Link_Start] = str_replace('{'.self::Link.'}',$this->_startString,$this->_Default_No_Link_Format);
			}else{
				$page = 1;
				$html = $this->_getALinkHtml($url,$page,$this->_startString,"strong");
				$pageHtml[self::Link_Start] = str_replace('{'.self::Link.'}',$html,$this->_Default_A_Link_Format);
			}
			
			if($this->_isHaveBackPage()){	
				$page = $this->_pageInfo->getPage()-1;
				$html = $this->_getALinkHtml($url,$page,$this->_backString,"strong");
				$pageHtml[self::Link_Back] = str_replace('{'.self::Link.'}',$html,$this->_Default_A_Link_Format);	
			}else{
				$pageHtml[self::Link_Back] = str_replace('{'.self::Link.'}',$this->_backString,$this->_Default_No_Link_Format);
			}
			
			if($this->_isHaveNextPage()){	
				$page = $this->_pageInfo->getPage()+1;
				$html = $this->_getALinkHtml($url,$page,$this->_nextString,"strong");
				$pageHtml[self::Link_Next] = str_replace('{'.self::Link.'}',$html,$this->_Default_A_Link_Format);	
			}else{
				$pageHtml[self::Link_Next] = str_replace('{'.self::Link.'}',$this->_nextString,$this->_Default_No_Link_Format);
			}
			
			if($this->_isLastPage()){	
				$pageHtml[self::Link_End] = str_replace('{'.self::Link.'}',$this->_endString,$this->_Default_No_Link_Format);
			}else{				
				$page = $this->_pageInfo->getTotalnumber();
				$html = $this->_getALinkHtml($url,$page,$this->_endString,"strong");
				$pageHtml[self::Link_End] = str_replace('{'.self::Link.'}',$html,$this->_Default_A_Link_Format);	
			}			
			
			$pageHtml[self::Link_List] = $this->_getLinklist($url);
			
			$this->_htmlPageLink = $pageHtml;			
		}catch (Exception $e){
			throw $e;
		}
		return $pageHtml;
	}
	
	protected function _isFirstPage(){
		if($this->_pageInfo->getPage() == 1){
			return true;
		}
		return false;
	}
	
	protected function _isHaveBackPage(){
		if($this->_pageInfo->getPage() > 1){
			return true;
		}
		return false;
	}
	
	protected function _isHaveNextPage(){
		if($this->_pageInfo->getTotalnumber() > $this->_pageInfo->getPage()){
			return true;
		}
		return false;
	}
	
	protected function _isLastPage(){
		if($this->_pageInfo->getTotalnumber() == $this->_pageInfo->getPage()){
			return true;
		}
		return false;
	}
	/**
	 * 根据page html的格式 产生page html
	 *
	 * @param array $pageHtml
	 * @return string html
	 */
	public function getPageFormatHtml($pageHtml = null){
		try{
			if(empty($pageHtml)){
				$pageHtml = empty($this->_htmlPageLink)?$this->getPageHtml():$this->_htmlPageLink;
			}
			if(empty($pageHtml)){
				return null;
			}
			$formatHtml = $this->_Default_Html_Format ;
			$formatHtml = str_replace('{'.self::Link_Start.'}',$pageHtml[self::Link_Start],$formatHtml);
			$formatHtml = str_replace('{'.self::Link_Back.'}',$pageHtml[self::Link_Back],$formatHtml);
			$formatHtml = str_replace('{'.self::Link_Next.'}',$pageHtml[self::Link_Next],$formatHtml);
			$formatHtml = str_replace('{'.self::Link_End.'}',$pageHtml[self::Link_End],$formatHtml);
			$formatHtml = str_replace('{'.self::Link_List.'}',$pageHtml[self::Link_List],$formatHtml);
			$formatHtml = str_replace('{'.self::Total_Page.'}',$this->_pageInfo->getTotalnumber(),$formatHtml);
			$formatHtml = str_replace('{'.self::Now_Page.'}',$this->_pageInfo->getPage(),$formatHtml);			
		}catch (Exception $e){
			throw $e;
		}
		return $formatHtml;
	}
		
	private function _getALinkHtml($url,$page,$msg,$tag = null){
		if(!empty($tag)){
			$msg = "<".$tag.">".$msg."</".$tag.">";
		}		
		return "<a href='".$url."/page/".$page."'>".$msg."</a>";		
	}	
	
	private function _getLinklist($url){
		$start = $this->_pageInfo->getPage() - (int)($this->_pageInfo->getLinklist()/2) ;
		if($start < 1){
			$start = 1;
		}
		$end = $start + $this->_pageInfo->getLinklist() -1;
		if($end > $this->_pageInfo->getTotalnumber()){
			$start = $start + $this->_pageInfo->getTotalnumber() - $end;
			if($start < 1){
				$start = 1;
			}
			$end = $this->_pageInfo->getTotalnumber();
		}
		$html = '';
		if($this->_pageInfo->getTotalnumber() > 1){
			for($start ;$start < $end+1 ; $start++){
				if($start == $this->_pageInfo->getPage() ){
					$msg = str_replace('{'.self::Link_List.'}',$start,$this->_Default_NowLinkList_Format);
				}else{
					$msg = str_replace('{'.self::Link_List.'}',$this->_getALinkHtml($url,$start,$start),$this->_Default_LinkList_Format);
				}
				$html .= $msg;	
			}
		}else{
			$html = null;
		}
		return $html;
	}	
}